package com.dingdang.controller;


import com.dingdang.data.Result;
import com.dingdang.em.ResultEnum;
import com.dingdang.entity.CarIntention;
import com.dingdang.entity.CarVehicle;
import com.dingdang.mapper.CarIntentionMapper;
import com.dingdang.mapper.CarVehicleMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-12-20
 */
@RestController
@RequestMapping("intention")
@Api(value = "车辆估价")
public class CarIntentionController {
    @Resource
    private CarIntentionMapper carIntentionMapper;
    @Resource
    private CarVehicleMapper carVehicleMapper;

    @PutMapping("add")
    @ApiOperation(value = "车辆估价")
    @ApiImplicitParam(name = "tbAdvertise", value = "有意向卖车的实体类", required = true, paramType = "body")
    public Result addInfo(@RequestBody CarIntention carIntention) {

        try {
            int km = 100000;
            int km2 = 300000;
            Long kmDriving = carIntention.getKmDriving();
    /*        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date endTimeString = null;
            Date  nowtimeString = new Date();  //当前时间
            long days = 0;*/
            carIntentionMapper.insert(carIntention);
/*            endTimeString = sdf.parse(carIntention.getFirstTime().toString()); //某个时间
            long endTime = endTimeString.getTime();
            long now = nowtimeString.getTime();
            Calendar calen = Calendar.getInstance();
            long diff = now - endTime ;
            days = diff / (1000 * 60 * 60 * 24);//转换为天数;
            System.out.println(days);*/
            if (kmDriving <= km2) {
                if(kmDriving >= km) {
                    CarVehicle carVehicle2 = new CarVehicle();
                    carVehicle2.setTypeName("8.5成新");
                    carVehicleMapper.insert(carVehicle2);
                    return Result.ok().setData(carVehicle2.getTypeName());
                }else{
                    CarVehicle carVehicle3 = new CarVehicle();
                    carVehicle3.setTypeName("9.5成新");
                    carVehicleMapper.insert(carVehicle3);
                    return Result.ok().setData(carVehicle3.getTypeName());
                }
            } else {
                CarVehicle carVehicle3 = new CarVehicle();
                carVehicle3.setTypeName("7成新");
                carVehicleMapper.insert(carVehicle3);
                return Result.ok().setData(carVehicle3.getTypeName());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

}
